// pages/home/home.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight:wx.getSystemInfoSync()['statusBarHeight'],
    menuButtonHeight: wx.getMenuButtonBoundingClientRect().height,
    menuButtonWidth: wx.getMenuButtonBoundingClientRect().width,
    channels: [],
    channelsLoc: [],
    channelsAll: [],
    activeId: 0,
    activeIndex: 0,
    workList: [],
    nowYear: 0,
    dailog: false,
    editTap: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getChannels()
    this.setData({
      nowYear: new Date().getFullYear()
    })
  },
  editTapSwitch(){
    this.setData({
      editTap: !this.data.editTap
    })
  },
  deleteTab(e){
    if (this.data.activeIndex === e.target.dataset.index) {
      this.setData({
        activeIndex: 0,
        activeId: 0
      })
      this.getWorkList()
    }
    const res = this.data.channels.splice(e.target.dataset.index, 1)
    this.setData({
      channels: this.data.channels
    })
    this.savTab()
  },
  showDailog(){
    this.setData({
      dailog: true
    })
    this.getAllTab()
  },
  hideDailog(){
    this.setData({
      dailog: false,
      editTap: false
    })
  },
  getChannels() {
    wx.showLoading({
      title: '加载中',
    })
    wx.getStorage({
      key: 'channels',
      success: (res) => {
        console.log(res);
        this.setData({
          channels: res.data,
          activeId: res.data[0].id
        })
        wx.hideLoading()
      },
      fail: ()=>{
        wx.request({
            url: app.globalData.baseURL +  '/user/channels',
            method: 'GET',
            header: {
              Authorization: app.globalData.Authorization
            },
            success: ( res ) => {
              console.log(res);
              this.setData({
                channels: res.data.data.channels,
                activeId: res.data.data.channels[0].id
              })
              this.savTab()
            },
            complete: () => {
              wx.hideLoading()
            }
        })
      },
      complete: ()=>{
        this.getWorkList()
      }
    })
  },
  acitveTab(e) {
    console.log(e.target.dataset.id, e.target.dataset.index)
    this.setData({
      activeId: e.target.dataset.id,
      activeIndex: e.target.dataset.index,
      workList: [],
      dailog:false
    })
    this.getWorkList()
  },
  getWorkList(callback) {
    if (!this.data.dailog) {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseURL + '/articles',
        method: 'GET',
        data: {
          channel_id: this.data.activeId,
          with_top: 1,
          timestamp: +new Date()
        },
        header: {
          Authorization: app.globalData.Authorization
        },
        success: (res) => {
          console.log(res);
          this.setData({
            workList: [...this.data.workList, ...res.data.data.results]
          })
          wx.hideLoading()
          callback && callback()
        },
        complete: () => {
  
        }
      })
    }
  },
  savTab(){
    wx.setStorage({
      key: 'channels',
      data: this.data.channels
    })
  },
  getAllTab(){
    if (!this.data.channelsLoc.length) {
      wx.request({
        url: app.globalData.baseURL +  '/channels',
        method: 'GET',
        success: (res) => {
          // console.log(res.data.data.channels);
          this.setData({
            channelsLoc: res.data.data.channels
          })
          this.setAllTab()
        }
      })
    } else {
      this.setAllTab()
    }
  },
  setAllTab(){
    const res = []
    const ids = []
    this.data.channels.map(item=>{ids.push(item.id)})
    console.log(this.data.channelsLoc, this.data.channels);
    this.data.channelsLoc.forEach(item=>{
      console.log(ids.indexOf(item.id));
      // console.log(this.data.channels.indexOf(item));
      // console.log(item);
      if (ids.indexOf(item.id) === -1) {
        res.push(item)
      }
    })
    this.setData({
      channelsAll: res
    })
    console.log(res);
  },
  addTab(e){
    const res = this.data.channelsAll.find(item => item.id === e.target.dataset.id)
    console.log(res);
    this.setData({
      channels: [...this.data.channels, res]
    })
    this.setAllTab()
  },
  clickWork(e){
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      workList: []
    })
    this.getWorkList(()=>{
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getWorkList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})